package com.supermap.search.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.supermap.search.R;
import com.supermap.search.javabean.CollectBean;
import com.supermap.search.wigdet.CustomListView;
import com.supermap.search.wigdet.SwipeLayout;
import com.supermap.search.wigdet.SwipeLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by XueQing Wang on 2018/6/4.
 */

public class SwipeCollectAdapter extends BaseAdapter {

    private List<CollectBean> list;
    private Context context;


    public SwipeCollectAdapter(List<CollectBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            View view = LayoutInflater.from(context).inflate(R.layout.swipe_layout_noupcollect, null);
            holder = new ViewHolder(view);
            convertView = view;
            view.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        //解决多次调用getView方法  利用当OnLayout结束
        if (parent instanceof CustomListView) {
            if (((CustomListView) parent).isOnMeasure()) {
                return convertView;
            }
        }

        CollectBean bean = list.get(position);

        holder.build.setText(bean.getBuild());
        holder.floor.setText(bean.getFloor());
        holder.resName.setText(bean.getResName());
        holder.resType.setText(bean.getResType());

        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onDeleteClick(position);
                }
            }
        });
        holder.tvUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    SwipeLayoutManager.getInstance().closeCurrentLayout();
                    onClickListener.onUploadClick(position);
                }
            }
        });
        return convertView;
    }


    private OnClickListener onClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickListener {

        public void onDeleteClick(int position);
        public void onUploadClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.build)
        TextView build;
        @BindView(R.id.floor)
        TextView floor;
        @BindView(R.id.res_name)
        TextView resName;
        @BindView(R.id.res_type)
        TextView resType;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_upload)
        TextView tvUpload;
        @BindView(R.id.tv_info)
        TextView tvInfo;
        @BindView(R.id.swipeLayout)
        SwipeLayout swipeLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            tvInfo.setVisibility(View.GONE);
        }
    }
}
